function mediaFunctions(){

	$("#mediaBar").corner();
	
	$("#uploadMedia").bind('click', function(){
		
		$("#mediaUploadDialog").dialog({
			bgiframe: true,
			modal: false,
		});
		
		$('#mediaUploadDialog').dialog('option', 'height', '50%');
		$('#mediaUploadDialog').dialog('option', 'width', '50%');
		$('#mediaUploadDialog').dialog('option', 'position', 'center');
	});

	$('#mediaUploadDialog').bind('dialogclose', function(event, ui) {
        $(this).dialog('destroy');
	});
	
	$("#submit").bind('click', function(){
		
		var modality = $("#modality option:selected").text();
		if (modality == ""){
						
			var text = $("#dataUpload2").text();
						
			$("#noDataToUpload").empty();
			$("#noDataToUpload").html("<span>"+text+"</span>");
			$("#modality").css('background-color', '#F7BE81');
			$("#noDataToUpload").fadeIn("slow");
			setTimeout("hideUploadErrorMessage()", 5000);
			$("#modality").bind('click', function(){
							
				$("#modality").css('background-color', '#FFFFFF');
						
			});
			return false;
		}
					
		var organ = $("#organ option:selected").text();
		if (organ == ""){
						
			var text = $("#dataUpload3").text();
						
			$("#noDataToUpload").empty();
			$("#noDataToUpload").html("<span>"+text+"</span>");
			$("#organ").css('background-color', '#F7BE81');
			$("#noDataToUpload").fadeIn("slow");
			setTimeout("hideUploadErrorMessage()", 5000);
			$("#organ").bind('click', function(){
						
				$("#organ").css('background-color', '#FFFFFF');
						
			});
			return false;
		}
		
		
					
		var tags = $("#tags").val();
		if (tags == ""){
						
			var text = $("#dataUpload4").text();
						
			$("#noDataToUpload").empty();
			$("#noDataToUpload").html("<span>"+text+"</span>");
			$("#tags").css('background-color', '#F7BE81');
			$("#noDataToUpload").fadeIn("slow");
			setTimeout("hideUploadErrorMessage()", 5000);
			$("#tags").bind('focus', function(){
							
				$("#tags").css('background-color', '#FFFFFF');
						
			});
			return false;	
		}
		
		new AjaxUpload('submit', {
		
			action: '../controller/storeMedia.php',
			name: 'newfile',
			data: {
		
				modality : $("#modality option:selected").text(),
				organ : $("#organ option:selected").text(),
				tags : $("#tags").val(),
				user : $("#user").val(),
			},
			autoSubmit: true,
			responseType: XML,
			onChange: function(file, extension){},
			onSubmit: function(file, extension) {
				// disable upload button (or entire form), show wait GIF
				$("#uploadMediaForm").fadeOut("fast");
				$("#uploadWaitGrafic").show();
			},	
			onComplete: function(file, response) {
				// enable upload button, hide wait GIF
				$("#uploadWaitGrafic").hide();
				$("#uploadMediaForm").fadeIn("fast");
			},
			
		});
		
	});
		
	
}

function getCurrentMedia(){
	var lastDays = $("#portalTimeSelector option:selected").text();
	
	$("#mediaRoll").empty(); // empty MediaRoll
	$.ajax({
		type: "GET",
		url: "../controller/readMedia.php",
		data: "lastdays="+lastDays,
		
		beforeSend: function(xml){
			// zeige Wartegrafik??
			$("<p id='mediaWait'>").html("<img src='./themes/lib/waitUpload.gif' />").appendTo("#mediaRoll");
			$("#mediaWait").css('width', $("#blogRoll").css('width'));
			$("#mediaWait").css('height', $("#blogRoll").css('height'));
		},
		success: function(xml){
			// entferne Wartegrafik
			$("#mediaWait").remove();
			$(xml).find('media').each(function(){
			 	var id = $(this).find('mediaId').text();
				var mediaType = $(this).find('mediaType').text();
				var tags = $(this).find('tags').text();
			 	var modality = $(this).find('modality').text();
				var organ = $(this).find('organ').text();
				var thumbnail = $(this).find('thumbnail').text();
				var mediaLocator = $(this).find('mediaLocator').text();
				var mediaEditor = $(this).find('mediaEditor').text();
				var uploadDate = $(this).find('uploadDate').text();
				var windowCenter = $(this).find('windowCenter').text();
				var windowWidth = $(this).find('windowWidth').text();
				var laterality = $(this).find('laterality').text();
			 	var imageComment = $(this).find('imageComment').text();
				var contrast = $(this).find('contrast').text();
				var mrAquisitionType = $(this).find('mrAquisitionType').text();
				var sequenceName = $(this).find('sequenceName').text();
				var sliceThickness = $(this).find('sliceThickness').text();
				var kV = $(this).find('kV').text();
				var protocolName = $(this).find('protocolName').text();
				var kernel = $(this).find('kernel').text();
				var mrCoil = $(this).find('mrCoil').text();
				var flipAngle = $(this).find('flipAngle').text();
				var patPos = $(this).find('patPos').text();
				var sens = $(this).find('sens').text();
				var manufacturer = $(this).find('manufacturer').text();
				
				$("<a id='media"+id+"' class='media' mediaId='"+id+"' mediaType='"+mediaType+"' media='"+mediaLocator+"' caption='"+modality+organ+"'>").html("<img src='"+thumbnail+"' alt='text'/>").appendTo("#mediaRoll");
				
				$("#media"+id).wTooltip({content: "<span class='tooltip'><b>"+modality+" "+organ+"</b><br/>"+tags+"<br/>c:"+windowCenter+" w:"+windowWidth+"<br/>"+imageComment+"<br/>"+protocolName+"</span>"});
			
			 });
		},
		
		complete: function(xml){

			$(".media").each(function(){
				
				$(this).bind('click', function(){
						
						switch($(this).attr('mediaType')){
							
							case '0': window.open($(this).attr('media'), 'MIME File'); break; // Unknown MIME
							case '1': showOverlayMovie($(this).attr('media'), "media_"+$(this).attr('mediaId')); break; //MPEG Video
							case '2': showOverlayMovie($(this).attr('media'), "media_"+$(this).attr('mediaId')); break; // AVI Video
							case '3': showOverlayMovie($(this).attr('media'), "media_"+$(this).attr('mediaId')); break; // Other Video
							case '4': showOverlayAudio($(this).attr('media'), "media_"+$(this).attr('mediaId')); break; // MP3 Audio
							case '5': showOverlayAudio($(this).attr('media'), "media_"+$(this).attr('mediaId')); break; // WAV Audio
							case '6': window.open($(this).attr('media'), 'PDF File'); break; // PDF File
							case '7': showOverlayImage($(this).attr('media'), "media_"+$(this).attr('mediaId')); break; // WAV Audio
							
						}
					
				});
				
			});

		},
		
	});	

}

function showOverlayImage(pointer, id){

	$("<div id='imageView'>").html("<span />").appendTo("#overlayDiv");
	$("<p>").html("<img class='insertedImage' id='"+id+"' src='"+pointer+"' />").appendTo("#imageView");
	
	$("#imageView").bind('click', function(){
		
		$("#imageView").fadeOut("fast");
		$("#imageView").empty();
		$("#imageView").remove();
	
	});
	
	$("#imageView").fadeIn("fast");	
	
	/*
	 * Resize Image corresponding to the imageView
	 * rezise
	 */
	var imgWidth = $("#"+id).width();
	var imgHeight = $("#"+id).height();
	var overlayWidth = $("#imageView").width();
	var overlayHeight = $("#imageView").height();
	
	if (imgWidth > imgHeight){
		// Landscape image
		// calculate img / div ratio
		var ratio = overlayWidth / imgWidth;
		$("#"+id).width((imgWidth * ratio)-100);
		$("#"+id).height((imgHeight * ratio)-100);
		$("#"+id).css('margin-left', '50px');
		$("#"+id).css('margin-top', '50px');
		
	} else {
		// Portrait Image
		var ratio = overlayHeight / imgHeight;
		$("#"+id).width((imgWidth * ratio)-100);
		$("#"+id).height((imgHeight * ratio)-100);
		$("#"+id).css('margin-left', '50px');
		$("#"+id).css('margin-top', '50px');
		
	}
	
	/*
	 * Control Board
	 */

	
}

function hideUploadErrorMessage(){
	 $("#noDataToUpload").fadeOut("slow");
}

function showOverlayMovie(pointer, id){
	
	$("<div id='mediaView'>").html("<span />").appendTo("#overlayDiv");
	$("<p class='insertedMediaFrame'>").html("<embed class='insertedMedia' id='"+id+"' src='"+pointer+"' autostart='true' loop='true' />").appendTo("#mediaView");

	/*var embedHeight = $(".insertedMedia").height();
	 *var embedWidth = $("#"+id).width();
	 *var embedHeight = embedHeight/2;
	 *var embedWidth = embedWidth/2;

	 *var overlayHeight = $("#mediaView").height();
	 *var overlayWidth = $("#mediaView").width();
	
	  *var left = parseFloat((overlayWidth - (embedWidth))/2-200).toFixed(0); // -200 is a dummy value because I couldn't select the embed size
	  *var top =  parseFloat((overlayHeight - (embedHeight))/2-200).toFixed(0); // -200 is a dummy value because I couldn't select the embed size
	
	  *$("#"+id).css('margin-left', left+"px");
	  *$("#"+id).css('margin-top', top+"px");
	  */
	
	 
	
	$("#mediaView").bind('click', function(){
		
		$("#mediaView").fadeOut("fast");
		$("#mediaView").empty();
		$("#mediaView").remove();
	
	});
	
	$("#mediaView").fadeIn("fast");	

}

function showOverlayAudio(pointer, id){
	
	$("<div id='mediaView'>").html("<span />").appendTo("#overlayDiv");
	$("<p>").html("<embed class='insertedMedia' id='"+id+"' src='"+pointer+"' autostart='true' loop='false' />").appendTo("#mediaView");

	var embedHeight = $(".insertedMedia").height();
	var embedWidth = $("#"+id).width();
	var embedHeight = embedHeight/2;
	var embedWidth = embedWidth/2;

	var overlayHeight = $("#mediaView").height();
	var overlayWidth = $("#mediaView").width();
	
	var left = parseFloat((overlayWidth - (embedWidth))/2-200).toFixed(0); // -200 is a dummy value because I couldn't select the embed size
	var top =  parseFloat((overlayHeight - (embedHeight))/2-200).toFixed(0); // -200 is a dummy value because I couldn't select the embed size
	
	$("#"+id).css('margin-left', left+"px");
	$("#"+id).css('margin-top', top+"px");
	
	$("#mediaView").bind('click', function(){
		
		$("#mediaView").fadeOut("fast");
		$("#mediaView").empty();
		$("#mediaView").remove();
	
	});
	
	$("#mediaView").fadeIn("fast");	

}